dojo.provide("uws.drawing.sequence.Loop");
dojo.require('uws.drawing.Connection');

dojo.declare('uws.drawing.sequence.Loop', uws.drawing.Connection,{
	constructor: function(id, surface, shape1, shape1_dir, shape2, shape2_dir, stereotype){
		this.umltype = "loop";
		
		var points = [];
		var arrowPoints = [{x:0,y:0}, {x:1,y:1}];
		this.shape1_dir = shape1_dir;
		this.shape2_dir = shape2_dir;
		
		if(shape1_dir=="top"){
			if(shape2_dir=="top"){		//same
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1, y:this.y1 - 30},
					{x:this.x1 + 30, y:this.y1 - 30},
					{x:this.x1 + 30, y:this.y1 - 10},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
			}else if(shape2_dir=="bottom"){		//opp
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1, y:this.y1 - 30},
					{x:this.x1 + 100, y:this.y1 - 30},
					{x:this.x1 + 100, y:this.y1 + 80},
					{x:this.x2, y:this.y2 + 30},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
			}else if(shape2_dir=="right"){		//adj
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1, y:this.y1 - 30},
					{x:this.x1 + 100, y:this.y1 - 30},
					{x:this.x2 + 30, y:this.y2 - 15},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-1], points[points.length-2]);
			}else if(shape2_dir=="left"){		//adj
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1, y:this.y1 - 30},
					{x:this.x1 - 100, y:this.y1 - 30},
					{x:this.x2 - 30, y:this.y2 - 15},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-1], points[points.length-2]);
			}
		}else if(shape1_dir=="bottom"){
			if(shape2_dir=="top"){		//opp
				points = [
					{x:this.x2, y:this.y2},
					{x:this.x2, y:this.y2 - 30},
					{x:this.x2 + 100, y:this.y2 - 30},
					{x:this.x2 + 100, y:this.y2 + 80},
					{x:this.x1, y:this.y1 + 30},
					{x:this.x1, y:this.y1}
				];
				arrowPoints = this.getArrowCoordinates(points[1], points[0]);
			}else if(shape2_dir=="bottom"){		//same
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1, y:this.y1 + 30},
					{x:this.x1 + 30, y:this.y1 + 30},
					{x:this.x1 + 30, y:this.y1 + 10},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
			}else if(shape2_dir=="right"){		//adj
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1, y:this.y1 + 30},
					{x:this.x1 + 100, y:this.y1 + 30},
					{x:this.x2 + 30, y:this.y2 + 15},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-1], points[points.length-2]);
			}else if(shape2_dir=="left"){		//adj
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1, y:this.y1 + 30},
					{x:this.x1 - 100, y:this.y1 + 30},
					{x:this.x2 - 30, y:this.y2 + 15},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-1], points[points.length-2]);
			}
		}else if(shape1_dir=="right"){
			if(shape2_dir=="top"){		//adj
				points = [
					{x:this.x2, y:this.y2},
					{x:this.x2 + 30, y:this.y2 - 30},
					{x:this.x2 + 100, y:this.y2 - 30},
					{x:this.x1 + 30, y:this.y1},
					{x:this.x1, y:this.y1}
				];
				arrowPoints = this.getArrowCoordinates(points[0], points[1]);
			}else if(shape2_dir=="bottom"){		//adj
				points = [
					{x:this.x2, y:this.y2},
					{x:this.x2 + 30 , y:this.y2 + 30},
					{x:this.x2 + 100, y:this.y2 + 30},
					{x:this.x1 + 30, y:this.y1},
					{x:this.x1, y:this.y1}
				];
				arrowPoints = this.getArrowCoordinates(points[0], points[1]);
			}else if(shape2_dir=="right"){		//same
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1 + 30, y:this.y1},
					{x:this.x1 + 30, y:this.y1 - 30},
					{x:this.x1 + 10, y:this.y1 - 30},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
			}else if(shape2_dir=="left"){		//opp
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1 + 30, y:this.y1},
					{x:this.x1 + 30, y:this.y1 + 40},
					{x:this.x2 - 30, y:this.y2 + 40},
					{x:this.x2 - 30, y:this.y2},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
			}
		}else if(shape1_dir=="left"){
			if(shape2_dir=="top"){		//adj
				points = [
					{x:this.x2, y:this.y2},
					{x:this.x2 - 30, y:this.y2 - 30},
					{x:this.x2 - 100, y:this.y2 - 30},
					{x:this.x1 - 30, y:this.y1},
					{x:this.x1, y:this.y1}
				];
				arrowPoints = this.getArrowCoordinates(points[0], points[1]);
			}else if(shape2_dir=="bottom"){		//adj
				points = [
					{x:this.x2, y:this.y2},
					{x:this.x2 - 30, y:this.y2 + 30},
					{x:this.x2 - 100, y:this.y2 + 30},
					{x:this.x1 - 30, y:this.y1},
					{x:this.x1, y:this.y1}
				];
				arrowPoints = this.getArrowCoordinates(points[0], points[1]);
			}else if(shape2_dir=="right"){		//opp
				points = [
					{x:this.x2, y:this.y2},
					{x:this.x2 + 30, y:this.y2},
					{x:this.x2 + 30, y:this.y2 + 40},
					{x:this.x1 - 30, y:this.y1 + 40},
					{x:this.x1 - 30, y:this.y1},
					{x:this.x1, y:this.y1}
				];
				arrowPoints = this.getArrowCoordinates(points[1], points[0]);
			}else if(shape2_dir=="left"){		//same
				points = [
					{x:this.x1, y:this.y1},
					{x:this.x1 - 30, y:this.y1},
					{x:this.x1 - 30, y:this.y1 - 30},
					{x:this.x1 - 10, y:this.y1 - 30},
					{x:this.x2, y:this.y2}
				];
				arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
			}
		}
		
		this.shapeCon = this.group.createPolyline(points).setStroke({color: "black",width:2});
		
		this.shapeCon2 = this.group.createLine({
			x1:arrowPoints[1].x,
			y1:arrowPoints[1].y,
			x2:this.x2,
			y2:this.y2
		}).setStroke({color:"black"});
		
		this.shapeCon3 = this.group.createLine({
			x1:arrowPoints[2].x,
			y1:arrowPoints[2].y,
			x2:this.x2,
			y2:this.y2
		}).setStroke({color:"black"});
		
		this.shape1_monitor = dojo.connect(shape1.shape_moveable,"onMoving",this,function(mover,shift){
			if (mover.shape.matrix == null) {
				mover.shape.matrix = {
					dx: 0,
					dy: 0
				};
			}
			
			var arrowPoints = [{x:0,y:0}, {x:1,y:1}];
			this.x2=(this.con_x2 + mover.shape.matrix.dx);
			this.y2=(this.con_y2 + mover.shape.matrix.dy);
			
			if(this.shape1_dir=="top"){
				if(this.shape2_dir=="top"){		//same
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy) - 10},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					
					arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
				}else if(shape2_dir=="bottom"){		//opp
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 100, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 100, y:(this.con_y1 + mover.shape.matrix.dy) + 80},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
				}else if(shape2_dir=="right"){		//adj
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 100, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 30, y:(this.con_y2 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-1], points[points.length-2]);
				}else if(shape2_dir=="left"){		//adj
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 100, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 30, y:(this.con_y2 + mover.shape.matrix.dy)-30},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-1], points[points.length-2]);
				}
			}else if(shape1_dir=="bottom"){
				if(shape2_dir=="top"){		//opp
					points = [
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 100, y:(this.con_y2 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 100, y:(this.con_y2 + mover.shape.matrix.dy) + 80},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[1], points[0]);
				}else if(shape2_dir=="bottom"){		//same
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy) + 10},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
				}else if(shape2_dir=="right"){		//adj
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 100, y:(this.con_y1 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 30, y:(this.con_y2 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
				}else if(shape2_dir=="left"){		//adj
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 100, y:(this.con_y1 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 30, y:(this.con_y2 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-1], points[points.length-2]);
				}
			}else if(shape1_dir=="right"){
				if(shape2_dir=="top"){		//adj
					points = [
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 30, y:(this.con_y2 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 100, y:(this.con_y2 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[1], points[0]);
				}else if(shape2_dir=="bottom"){		//adj
					points = [
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 30, y:(this.con_y2 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 100, y:(this.con_y2 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[1], points[0]);
				}else if(shape2_dir=="right"){		//same
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 10, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
				}else if(shape2_dir=="left"){		//opp
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx) + 30, y:(this.con_y1 + mover.shape.matrix.dy) + 40},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 30, y:(this.con_y2 + mover.shape.matrix.dy) + 40},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 30, y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
				}
			}else if(shape1_dir=="left"){
				if(shape2_dir=="top"){		//adj
					points = [
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 30, y:(this.con_y2 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 100, y:(this.con_y2 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[0], points[1]);
				}else if(shape2_dir=="bottom"){		//adj
					points = [
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 30, y:(this.con_y2 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x2 + mover.shape.matrix.dx) - 100, y:(this.con_y2 + mover.shape.matrix.dy) + 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[0], points[1]);
				}else if(shape2_dir=="right"){		//opp
					points = [
						{x:(this.con_x2 + mover.shape.matrix.dx), y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 30, y:(this.con_y2 + mover.shape.matrix.dy)},
						{x:(this.con_x2 + mover.shape.matrix.dx) + 30, y:(this.con_y2 + mover.shape.matrix.dy) + 40},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 30, y:(this.con_y1 + mover.shape.matrix.dy) + 40},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[1], points[0]);
				}else if(shape2_dir=="left"){		//same
					points = [
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 30, y:(this.con_y1 + mover.shape.matrix.dy)},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 30, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx) - 10, y:(this.con_y1 + mover.shape.matrix.dy) - 30},
						{x:(this.con_x1 + mover.shape.matrix.dx), y:(this.con_y1 + mover.shape.matrix.dy)}
					];
					arrowPoints = this.getArrowCoordinates(points[points.length-2], points[points.length-1]);
				}
			}
			
			this.shapeCon.setShape(points);
			
			this.x1=(this.con_x1 + mover.shape.matrix.dx);
			this.y1=(this.con_y1 + mover.shape.matrix.dy);
			
			this.shapeCon2.setShape({
				x1:arrowPoints[1].x,
				y1:arrowPoints[1].y,
				x2:this.x2,
				y2:this.y2
			}).setStroke({color:"black"});
			
			this.shapeCon3.setShape({
				x1:arrowPoints[2].x,
				y1:arrowPoints[2].y,
				x2:this.x2,
				y2:this.y2
			}).setStroke({color:"black"});
		});
		
	},
	applyProperties:function(properties){
		
	}
});